package com.stedu.controller;

import com.stedu.bean.Borrow;
import com.stedu.bean.RespBean;
import com.stedu.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/borrow")
public class BorrowController {
    @Autowired
    private BorrowService borrowService;

    //添加
    @PostMapping
    public RespBean insertController(@RequestBody Borrow borrow){
        if (borrowService.insertBo(borrow)){
            return RespBean.ok("添加完成");
        }else{
            return RespBean.error("添加失败");
        }
    }

    //删除
    @DeleteMapping("/{boid}")
    public RespBean deleteController(@PathVariable Integer boid){
        if (borrowService.deleteBo(boid)){
            return RespBean.ok("删除成功");
        }else{
            return RespBean.error("该记录表不存在");
        }
    }

    //修改
    @PutMapping
    public RespBean updateController(@RequestBody Borrow borrow){
        if (borrowService.updateBo(borrow)){
            return RespBean.ok("修改成功");
        }else{
            return RespBean.error("修改失败");
        }
    }

    @PutMapping("/a")
    public RespBean updateController1(@RequestBody Borrow borrow){
        if (borrowService.updateBo1(borrow)){
            return RespBean.ok("修改成功");
        }else{
            return RespBean.error("修改失败");
        }
    }

    //查询所有
    @GetMapping
    public RespBean selectAllController(){
        List<Borrow> borrows = borrowService.selectAll();
        return RespBean.ok("查询成功", borrows);
    }

    //根据id查询
    @GetMapping("/ByBoid/{boid}")
    public RespBean selectByBoidController(@PathVariable Integer boid){
        Borrow borrow = borrowService.selectById(boid);
        return RespBean.ok("查询成功", borrow);
    }

}
